<?php
/**
 * Created by PhpStorm.
 * User: 我型我素
 * Date: 2018/3/21
 * Time: 16:20
 */

namespace SDXApi\Service;


class BonusService
{
    protected $bonusTypeModel;
    protected $bonusUserModel;
    protected $bonusReceiveRuleModel;

    public function __construct()
    {
        $this->bonusTypeModel = M('bonus_type');
        $this->bonusUserModel = D('Userbonus');
        $this->bonusReceiveRuleModel = M('bonus_receive_rule');
    }

    /**
     * 获取红包数量信息
     * @author yangzhic
     * @param $bonusTypeId
     * @return array
     */
    public function getBonusNumber($bonusTypeId){
        $data = array();

        $bonusTypeInfo = $this->getBonusTypeInfo($bonusTypeId);

        $data['total_number'] = $this->getBonusTotalNumber($bonusTypeInfo);
        $data['remainder_number'] = $this->getBonusRemainderNumber($bonusTypeInfo,$data['total_number']);
        if($data['total_number']>0){
            $data['rate'] = round($data['remainder_number']/$data['total_number']*100,2);
        }else{
            $data['rate'] = 0;
        }


        return $data;
    }

    /**
     * 获取红包总数量
     * @author yangzhic
     * @param $bonusTypeInfo
     * @return float|int
     */
    public function getBonusTotalNumber($bonusTypeInfo){
        $total_number = $this->bonusUserModel->getBonusTotalNumber($bonusTypeInfo['type_id']);
        if($bonusTypeInfo['is_use_virtual_num']==1){
            $total_number = $total_number * $bonusTypeInfo['virtual_num_multiple'];
        }
        return $total_number;
    }

    /**
     * 获取红包剩余数量
     * @author yangzhic
     * @param $bonusTypeInfo
     * @param $BonusTotalNumber
     * @return float|int
     */
    public function getBonusRemainderNumber($bonusTypeInfo,$BonusTotalNumber){
        $virtual_num = S('bonus_virtual_num_'.$bonusTypeInfo['type_id']);

        if($bonusTypeInfo['is_use_virtual_num']==1){
            if($virtual_num === false){
                $virtual_num = $BonusTotalNumber;
                S('bonus_virtual_num_'.$bonusTypeInfo['type_id'],$virtual_num);
            }
        }else{
            $virtual_num = 0;
        }


        $real_num = $this->getBonusRealRemainderNumber($bonusTypeInfo['type_id']);

        if($virtual_num < $real_num || $real_num <= 0){
            $remainder_number = $real_num;
        }else{
            $remainder_number = $virtual_num;
        }

        return $remainder_number;
    }


    /**
     * 获取红包信息
     * @author yangzhic
     * @param $bonusTypeId
     * @return mixed
     */
    private function getBonusTypeInfo($bonusTypeId){
        $map = array('type_id'=>$bonusTypeId);
        $bonusTypeInfo = $this->bonusTypeModel->where($map)->find();
        return $bonusTypeInfo;
    }

    /**
     * 获取红包实际剩余数量
     * @author yangzhic
     * @param $bonusTypeId
     * @return mixed
     */
    private function getBonusRealRemainderNumber($bonusTypeId){
        $map = array('bonus_type_id'=>$bonusTypeId,'user_id'=>0);
        return $this->bonusUserModel->where($map)->count('bonus_id');
    }

    /**
     * 获取红包实际已领取数量
     * @author yangzhic
     * @param $bonusTypeId
     * @return mixed
     */
    private function getHasGetNumber($bonusTypeId){
        $map = array('bonus_type_id'=>$bonusTypeId,'user_id'=>array('gt',0));
        return $this->bonusUserModel->where($map)->count('bonus_id');
    }

    public function checkVirtualNumber(){
        $current_time = gmtime();

        $map = array(
            'is_use_virtual_num'=>1,
            'send_start_date'=>array('elt',$current_time),
            'send_end_date'=>array('egt',$current_time)
        );

        $bonus_list = $this->bonusTypeModel->where($map)->select();

        if(!empty($bonus_list)){
            foreach($bonus_list as $key=>$val){
                //获取虚拟剩余红包数量
                $virtual_num = S('bonus_virtual_num_'.$val['type_id']);
                if($virtual_num === false){
                    //获取红包实际总数量
                    $real_total_number = $this->bonusUserModel->getBonusTotalNumber($val['type_id']);
                    $virtual_num = $real_total_number * $val['virtual_num_multiple'];
                }
                if($virtual_num==0){
                    continue;
                }
                if($virtual_num<0){
                    S('bonus_virtual_num_'.$val['type_id'],0);
                    continue;
                }

                //是否在设置区间
                $receive_rule = $this->getBonusReceiveRule($val,$current_time);
                if(empty($receive_rule)){
                    continue;
                }

                $rand_num = rand($receive_rule['start_rand'],$receive_rule['end_rand']);

                $real_receive_num = $this->getHasGetNumber($val['type_id']);
                $virtual_num = $virtual_num-$rand_num-$real_receive_num;

                S('bonus_virtual_num_'.$val['type_id'],$virtual_num);
            }
        }
    }

    /**
     * 获取符合条件的红包设置
     * @author yangzhic
     * @param $bonus
     * @param $current_time
     * @return array
     */
    private function getBonusReceiveRule($bonus,$current_time){
        $data = array();
        $hour = intval(date('Hi',localtotime($current_time)));

        $map = array('bonus_type_id'=>$bonus['type_id']);
        $receive_rules = $this->bonusReceiveRuleModel->where($map)->select();

        if(!empty($receive_rules)){
            foreach ($receive_rules as $key=>$rule){
                $start_hour = intval(str_replace(':','',$rule['start_hour']));
                $end_hour = intval(str_replace(':','',$rule['end_hour']));
                if($hour>=$start_hour && $hour<=$end_hour){
                    $data = $rule;
                    break;
                }
            }
        }

        return $data;
    }
}